package com.teamwings.ctrls.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teamwings.common.vo.Result;
import com.teamwings.ctrls.entity.OatAppUrge;
import com.teamwings.ctrls.service.OatAppUrgeService;
import com.teamwings.ctrls.vo.OatAppUrgeAddVO;
import com.teamwings.ctrls.where.OatAppUrgeWhere;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.util.WhereUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * <p>
 * 催办管理控制器
 * </p>
 *
 * @author jobob
 * @since 2021-05-05
 */
@RestController
@Api(tags = "催办管理")
@RequestMapping("/form/urge")
public class UrgeController {

    @Autowired
    private OatAppUrgeService service;

    /**
     * 新增催办
     *
     */
    @ApiOperation(value = "新增催办", notes = "")
    @PutMapping()
    public Result<?> add(@Valid @RequestBody OatAppUrgeAddVO urgeAddVO, BindingResult results) {
        // @Valid开启校验 判断如果有不符合检验的，返回检验信息
        if (results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
        OatAppUrge urge = new OatAppUrge();
        BeanUtils.copyProperties(urgeAddVO,urge);
        boolean b = service.save(urge);
        if (!b) {
            Result.error("新增失败");
        }
        return Result.OK("设置催办成功");
    }

    /**
     * 查询催办列表
     *
     */
    @ApiOperation(value = "查询催办列表", notes = "")
    @GetMapping()
    public Result<?> list( OatAppUrgeWhere urgeWhere) throws IllegalAccessException {

        QueryWrapper<OatAppUrge> queryWrapper = new QueryWrapper<>();
        // 拼接查询条件
        WhereUtil.convertQuery(queryWrapper,urgeWhere);
        // 如果有页码，则进行分页查询，默认每页 20 条，可传参修改
        if (urgeWhere.getPageNo() != null ){
            Page<OatAppUrge> page = new Page(urgeWhere.getPageNo(), urgeWhere.getPageSize());
            return Result.OK(service.page(page, queryWrapper));
        }
        // 没有侧进行普通查询
        return Result.OK(service.list(queryWrapper));
    }


}
